<?php

namespace App\Http\Controllers;

use App\Exports\Flight\FlightDynamicDelayExport;
use App\Exports\Flight\FlightDynamicExport;
use App\Exports\Flight\FlightPriceSpaceExport;
use App\Exports\Flight\SevenDynamicDelayExport;
use App\Exports\Flight\SevenDynamicExport;
use App\Exports\Flight\SevenDynamicSplitExport;
use App\Exports\Flight\SevenScheduleExport;
use App\Repositories\Contracts\FlightInterface;
use App\Services\Flight\FlightSearchService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;

class FlightController extends BaseController
{
    public function __construct(FlightInterface $interface)
    {
        parent::__construct($interface);
    }

    /**
     * 列表
     * @param Request $request
     * @return \MakeRep\Resources\BaseCollection
     */
    public function ajaxLists(Request $request)
    {
        $pageSize = $request->page_size ? : 10;

        $flightDynamicService = new FlightSearchService();
        $whereIn = $flightDynamicService->searchFlightDynamic($request);
        $res = $this->interface->pageLists([],'*',$pageSize,'id','desc','id',$whereIn);

        return new BaseCollection($res);
    }

    /**
     * 数据导出
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function download(){

        ini_set('memory_limit', '1024M');

        return Excel::download(new FlightDynamicExport(),'航班动态'.date('YmdHis').'.xlsx');
    }

    /**
     * 数据导出
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function downloadDelay(){

        ini_set('memory_limit', '1024M');

        return Excel::download(new FlightDynamicDelayExport(),'30天延误'.date('YmdHis').'.xlsx');
    }

    /**
     * 30天延误
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function downloadSevenData(){

        ini_set('memory_limit', '1024M');

        return Excel::download(new SevenDynamicExport(),'7天内航班动态'.date('YmdHis').'.xlsx');
    }

    /**
     * 7天延误
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function downloadSevenDataDelay(){

        ini_set('memory_limit', '1024M');

        return Excel::download(new SevenDynamicDelayExport(),'7天延误'.date('YmdHis').'.xlsx');
    }

    /**
     * 7天调时
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function downloadSevenSchedule(){

        ini_set('memory_limit', '1024M');

        return Excel::download(new SevenScheduleExport(),'7天调时'.date('YmdHis').'.xlsx');
    }

    /**
     * 价格空间
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadPriceSpace(){

        ini_set('memory_limit', '1024M');
        return Excel::download(new FlightPriceSpaceExport(),'价格空间'.date('YmdHis').'.xlsx');
    }

    //7天数据分割
    public function sevenData(){
        ini_set('memory_limit', '2048M');
        return Excel::download(new SevenDynamicSplitExport(),'7天数据'.date('YmdHis').'.xlsx');
    }
}